const app = getApp()
import {urls} from '../../../utils/urls'
let _this;
Page({
  data: {
    task_list1:[],
    task_list2:[],
    page1:1,
    page2:1,
    load:false,
    size1:5,
    size2:5,
    tag:['我发布的','我帮助的'],
    flag:0,
    url:'get',
    user_id:wx.getStorageSync("user").id,
    count : wx.getStorageSync('task').count
  },
  onLoad: function () {
    _this = this
    _this.getMyList(0)
    _this.getHelpList(0)
  },
  getMyList(type){
    if (type == 0) {
      this.data.page1 = 1
    } else {
      this.data.page1 += 1
    }
    this.setData({
      load:true
    })
    let params1 ={
      page: this.data.page1,
      size: this.data.size1,
      producer : this.data.user_id
    }
    app.http('GET',urls.task ,params1, 
    function (res) {
      wx.stopPullDownRefresh()
      if (res.code == 100) {
        let new_task1 = res.results.results
        // 时间转换
        for (let i in new_task1) {
          new_task1[i].create_time = app.formatMsgTime(new_task1[i].create_time)
        }
        let task1 = []
        if (type == 0) {
          for (let i in new_task1) {
             task1.push(new_task1[i]) 
          }
        } else {
          task1 = _this.data.task_list1
          for (let i in new_task1) {
              task1.push(new_task1[i])           
          }
        }
        _this.setData({
          task_list1: task1,
          count1: res.results.count1,
          load:false
        })
      } else {
        _this.setData({
          load: false
        })
        wx.showToast({
          title: '到底了！',
          icon: 'none'
        })
      }
    })
  },
  getHelpList(type){
    if (type == 0) {
      this.data.page2 = 1
    } else {
      this.data.page2 += 1
    }
    this.setData({
      load:true
    })
    let params2 ={
      page: this.data.page2,
      size: this.data.size2,
      consumer : this.data.user_id
    }
    app.http('GET',urls.task ,params2, 
    function (res) {
      wx.stopPullDownRefresh()
      if (res.code == 100) {
        let new_task2 = res.results.results
        // 时间转换
        for (let i in new_task2) {
          new_task2[i].create_time = app.formatMsgTime(new_task2[i].create_time)
        }
        let task2 = []
        if (type == 0) {
          for (let i in new_task2) {
             task2.push(new_task2[i]) 
          }
        } else {
          task2 = _this.data.task_list2
          for (let i in new_task2) {
              task2.push(new_task2[i])           
          }
        }
        _this.setData({
          task_list2: task2,
          count2: res.results.count2,
          load:false
        })
      } else {
        _this.setData({
          load: false
        })
        wx.showToast({
          title: '到底了！',
          icon: 'none'
        })
      }
    })
  },
  onShow: function () {
    _this.getMyList(0)
    _this.getHelpList(0)
  },
  onPullDownRefresh: function () {
    _this.getMyList(0)
    _this.getHelpList(0)
  },
  onReachBottom: function () {
    if(this.data.task_list1.length < this.data.count1){
      _this.getMyList(1)
    }
    if(this.data.task_list2.length < this.data.count2){
      _this.getHelpList(1)
    }
  },
  changeTag(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      flag: index
    })
    if(index == 0){
      _this.getMyList(0)
    }else{
      _this.getHelpList(0)
    }
  },
  confirm(e){
    let update_dict ={
      id : e.currentTarget.dataset.id,
      task_status : 3,
    } 
    console.log(update_dict);
    app.http('PUT',urls.task, update_dict,
    function(res){
      if(res.code == 100){
        wx.showToast({
          title: '收货成功！',
        })
      }
    })
    _this.getMyList(0)
  }
})